package online.sixfish.codegenerator.api;

import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ApiGenerator {

    public static void main(String[] args) {
        // 示例：生成 FaqTypeForm 类
        generateFormClass(FaqType.class);


    }

    private static void generateFormClass(Class<?> originalClass) {
        try {
            Field[] fields = originalClass.getDeclaredFields();

            List<String> importStatements = new ArrayList<>();
            StringBuilder sb = new StringBuilder();
            sb.append("package ").append(originalClass.getPackage().getName()).append(";\n\n");

            // Add necessary import statements
            for (Field field : fields) {
                String fieldType = field.getType().getName();
                if (!isJavaLangType(fieldType)) {
                    importStatements.add("import " + fieldType + ";");
                }
            }

            // Add Lombok import statement
            importStatements.add("import lombok.Data;");

            // Sort and add import statements
            importStatements.sort(String::compareTo);
            importStatements.forEach(sb::append);
            sb.append("\n");
            // Add @Data annotation and class definition
            sb.append("@Data\n");
            sb.append("public class ").append(originalClass.getSimpleName()).append("Form {\n");

            // Generate field declarations excluding specified fields (e.g., 'delFlag')
            String excludedFields = "delFlag"; // 可以根据需要修改或扩展这个字符串
            String fieldDeclarations = Arrays.stream(fields)

                    .filter(field -> !excludedFields.contains(field.getName()))
                    .map(field -> {
                        String fieldType = field.getType().getName();
                        // Remove "java.lang." prefix if present
                        fieldType = removeJavaLangPrefix(fieldType);
                        String fieldName = field.getName();
                        return String.format("    private %s %s;", fieldType, fieldName);
                    })
                    .collect(Collectors.joining("\n"));

            sb.append(fieldDeclarations).append("\n");
            sb.append("}\n");

            // Write the generated code to a file
            FileWriter writer = new FileWriter(originalClass.getSimpleName() + "Form.java");
            writer.write(sb.toString());
            writer.close();
            System.out.println(originalClass.getSimpleName() + "Form class generated successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static boolean isJavaLangType(String typeName) {
        return typeName.startsWith("java.lang.");
    }

    private static String removeJavaLangPrefix(String typeName) {
        return isJavaLangType(typeName) ? typeName.substring("java.lang.".length()) : typeName;
    }
}